home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-src / datatypes / dtgen.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  8KB  |  303 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct dtlist {char *spec,*descr;} dts[]={
  6. #include "datatypes.h"
  7. };
  8.  
  9. struct dtconvlist {char *from,*to,*filef,*filet;int size;} cnvs[]={
  10. #include "dtconv.h"
  11. };
  12.  
  13. int dtcnt=sizeof(dts)/sizeof(dts[0]);
  14. int cnvcnt=sizeof(cnvs)/sizeof(cnvs[0]);
  15.  
  16. char *have;
  17.  
  18. #define CHAR 1
  19. #define UCHAR 2
  20. #define SHORT 3 
  21. #define USHORT 4
  22. #define INT 5
  23. #define UINT 6
  24. #define LONG 7
  25. #define ULONG 8
  26. #define FLOAT 9
  27. #define DOUBLE 10
  28. #define POINTER 11
  29.  
  30. #define TYPECNT POINTER
  31.  
  32. char *typen[TYPECNT+1]={"error","char","uchar","short","ushort","int","uint",
  33.                 "long","ulong","float","double","pointer"};
  34. char *ftypen[TYPECNT+1]={"error","char","unsigned char","short","unsigned short",
  35.                                  "int","unsigned int","long","unsigned long",
  36.                                  "float","double","char *"};
  37.  
  38. int dt[TYPECNT+1],cnv[TYPECNT+1];
  39. char *nt[TYPECNT+1];
  40. FILE *fin,*cout,*hout;
  41. int crosscompiler;
  42.  
  43. void *mymalloc(size_t size)
  44. {
  45.   void *p=malloc(size);
  46.   if(!p){
  47.     printf("Out of memory!\n");
  48.     exit(EXIT_FAILURE);
  49.   }
  50.   return p;
  51. }
  52.  
  53. int askyn(void)
  54. {
  55.   char in[8];
  56.   do{
  57.     printf("Type y or n: ");
  58.     fflush(stdout);
  59.     fgets(in,127,stdin);
  60.   }while(*in!='y'&&*in!='n');
  61.   return *in=='y';
  62. }
  63.  
  64. char *asktype(void)
  65. {
  66.   char *in=mymalloc(128);
  67.   printf("Enter that type: ");
  68.   fflush(stdout);
  69.   fgets(in,127,stdin);
  70.   if(in[strlen(in)-1]=='\n') in[strlen(in)-1]=0;
  71.   return in;
  72. }
  73.  
  74. int tst(int type,char *spec)
  75. {
  76.   int i,j;
  77.   for(i=0;i<dtcnt;i++){
  78.     if(strstr(spec,dts[i].spec)){ 
  79.       if(have[i]==-2) continue;
  80.       if(have[i]>=0){
  81. /*     printf("auto: %s == %s\n",dts[i].spec,nt[have[i]]); */
  82.     dt[type]=i;
  83.     nt[type]=nt[have[i]];
  84.     cnv[type]=-1;
  85.     return 1;
  86.       }else{
  87.     printf("Does your system/compiler support a type implemented as\n%s?\n",dts[i].descr);
  88.     if(askyn()){
  89.       dt[type]=i;
  90.       nt[type]=asktype();
  91.       have[i]=type;
  92.       cnv[type]=-1;
  93.       return 1;
  94.     }else{
  95.       have[i]=-2;
  96.     }
  97.       }
  98.     }
  99.   }
  100.   for(j=0;j<cnvcnt;j++){
  101.     char *s=0;
  102.     if(strstr(spec,cnvs[j].from)) s=cnvs[j].to;
  103. /*     if(strstr(spec,cnvs[j].to)) s=cnvs[j].from; */
  104.     if(s){
  105.       for(i=0;i<dtcnt;i++){
  106.     if(!strcmp(s,dts[i].spec)){
  107.       if(have[i]==-2) continue;
  108.       if(have[i]>=0){
  109.         dt[type]=i;
  110.         nt[type]=nt[have[i]];
  111.         cnv[type]=j;
  112.         return 2;
  113.       }else{
  114.         printf("Does your system/compiler support a type implemented as\n%s?\n",dts[i].descr);
  115.         if(askyn()){
  116.           dt[type]=i;
  117.           nt[type]=asktype();
  118.           have[i]=type;
  119.           cnv[type]=j;
  120.           return 2;
  121.         }else{
  122.           have[i]=-2;
  123.         }
  124.       }
  125.     }
  126.       }
  127.     }
  128.   }
  129.   return 0;
  130. }
  131.     
  132. char *castfrom(int type)
  133. {
  134.   if(cnv[type]>=0){
  135.     char *s=mymalloc(16);
  136.     sprintf(s,"dtcnv%df",type);
  137.     return s;
  138.   }else{
  139.     return "";
  140.   }
  141. }
  142. char *castto(int type)
  143. {
  144.   if(cnv[type]>=0){
  145.     char *s=mymalloc(16);
  146.     sprintf(s,"dtcnv%dt",type);
  147.     return s;
  148.   }else{
  149.     return "";
  150.   }
  151. void gen_cast(char *name,int from,int to)
  152. {
  153.   fprintf(hout,"#define %s(x) %s((%s)%s(x))\n",name,castto(to),nt[to],castfrom(from));
  154. }
  155. void gen_2op(char *name,char *op,int type)
  156. {
  157.   fprintf(hout,"#define %s(a,b) %s(%s(a)%s%s(b))\n",name,castto(type),castfrom(type),op,castfrom(type));
  158. void gen_1op(char *name,char *op,int type)
  159. {
  160.   fprintf(hout,"#define %s(a) %s(%s%s(a))\n",name,castto(type),op,castfrom(type));
  161. void gen_cmp(char *name,char *op,int type)
  162. {
  163.   fprintf(hout,"#define %s(a,b) (%s(a)%s%s(b))\n",name,castfrom(type),op,castfrom(type));
  164. main(int argc,char **argv)
  165. {
  166.   char type[128],spec[128];
  167.   int i,r;
  168.   if(argc!=4){ printf("Usage: dtgen <config-file> <output-file.h> <output-file.c>\n");exit(EXIT_FAILURE);}
  169. /*   printf("%d datatypes, %d conversions\n",dtcnt,cnvcnt); */
  170.   have=mymalloc(dtcnt*sizeof(*have));
  171.   memset(have,-1,sizeof(*have)*dtcnt);
  172.   fin=fopen(argv[1],"r");
  173.   if(!fin){ printf("Could not open <%s> for input!\n",argv[1]);exit(EXIT_FAILURE);}
  174.   hout=fopen(argv[2],"w");
  175.   if(!hout){ printf("Could not open <%s> for output!\n",argv[2]);exit(EXIT_FAILURE);}
  176.   cout=fopen(argv[3],"w");
  177.   if(!hout){ printf("Could not open <%s> for output!\n",argv[3]);exit(EXIT_FAILURE);}
  178.   printf("Are you building a cross-compiler?\n");
  179.   crosscompiler=askyn();
  180.   for(i=1;i<=TYPECNT;i++){
  181.     fgets(spec,127,fin);
  182. /*     printf("Specs for z%s:\n%s\n",typen[i],spec); */
  183.     if(!crosscompiler){
  184.       dt[i]=i;
  185.       nt[i]=ftypen[i];
  186.       have[i]=i;
  187.       cnv[i]=-1;
  188.     }else{
  189.       if(!tst(i,spec)){
  190.     printf("Problem! Your system does not seem to provide all of the data types\n"
  191.            "this version of vbcc needs.\nWrite to volker@vb.franken.de!\n");
  192.     exit(EXIT_FAILURE);
  193.       }
  194.     }
  195.   }
  196.   fprintf(hout,"\n\n/* Machine generated file. DON'T TOUCH ME! */\n\n\n");
  197.   fprintf(cout,"\n\n/* Machine generated file. DON'T TOUCH ME! */\n\n\n");
  198.   fprintf(cout,"#include \"dt.h\"\n\n");
  199.   for(i=1;i<=TYPECNT;i++){
  200.     if(cnv[i]>=0){
  201.       fprintf(hout,"typedef struct {char a[%d];} dt%df;\n",cnvs[cnv[i]].size,i);
  202.       fprintf(hout,"typedef dt%df z%s;\n",i,typen[i]);
  203.       fprintf(hout,"typedef %s dt%dt;\n",nt[i],i);
  204.       fprintf(hout,"dt%dt dtcnv%df(dt%df);\n",i,i,i);
  205.       fprintf(hout,"dt%df dtcnv%dt(dt%dt);\n",i,i,i);
  206.       fprintf(cout,"#undef DTTTYPE\n#define DTTTYPE dt%dt\n",i);
  207.       fprintf(cout,"#undef DTFTYPE\n#define DTFTYPE dt%df\n",i);
  208.       fprintf(cout,"dt%dt dtcnv%df(dt%df\n",i,i,i);
  209.       fprintf(cout,"#include \"%s\"\n",cnvs[cnv[i]].filef);
  210.       fprintf(cout,"dt%df dtcnv%dt(dt%dt\n",i,i,i);
  211.       fprintf(cout,"#include \"%s\"\n",cnvs[cnv[i]].filet);
  212.     }else{
  213.       fprintf(hout,"typedef %s z%s;\n",nt[i],typen[i]);
  214.     }
  215.   }
  216.  
  217.   gen_cast("zc2zl",CHAR,LONG);
  218.   gen_cast("zs2zl",SHORT,LONG);
  219.   gen_cast("zi2zl",INT,LONG);
  220.   gen_cast("zl2zc",LONG,CHAR);
  221.   gen_cast("zl2zs",LONG,SHORT);
  222.   gen_cast("zl2zi",LONG,INT);
  223.   gen_cast("zuc2zul",UCHAR,ULONG);
  224.   gen_cast("zus2zul",USHORT,ULONG);
  225.   gen_cast("zui2zul",UINT,ULONG);
  226.   gen_cast("zul2zuc",ULONG,UCHAR);
  227.   gen_cast("zul2zus",ULONG,USHORT);
  228.   gen_cast("zul2zui",ULONG,UINT);
  229.   gen_cast("zul2zl",ULONG,LONG);
  230.   gen_cast("zl2zul",LONG,ULONG);
  231.   gen_cast("zf2zd",FLOAT,DOUBLE);
  232.   gen_cast("zd2zf",DOUBLE,FLOAT);
  233.   gen_cast("zd2zl",DOUBLE,LONG);
  234.   gen_cast("zl2zd",LONG,DOUBLE);
  235.   gen_cast("zd2zul",DOUBLE,ULONG);
  236.   gen_cast("zul2zd",ULONG,DOUBLE);
  237.   gen_cast("zp2zul",POINTER,ULONG);
  238.   gen_cast("zul2zp",ULONG,POINTER);
  239.  
  240.   fprintf(hout,"#define l2zl(x) %s((%s)(x))\n",castto(LONG),nt[LONG]);
  241.   fprintf(hout,"#define ul2zul(x) %s((%s)(x))\n",castto(ULONG),nt[ULONG]);
  242.   fprintf(hout,"#define d2zd(x) %s((%s)(x))\n",castto(DOUBLE),nt[DOUBLE]);
  243.   fprintf(hout,"#define zl2l(x) ((long)%s(x))\n",castfrom(LONG));
  244.   fprintf(hout,"#define zul2ul(x) ((unsigned long)%s(x))\n",castfrom(ULONG));
  245.   fprintf(hout,"#define zd2d(x) ((double)%s(x))\n",castfrom(DOUBLE));
  246.  
  247.   gen_2op("zladd","+",LONG);
  248.   gen_2op("zuladd","+",ULONG);
  249.   gen_2op("zdadd","+",DOUBLE);
  250.   gen_2op("zlsub","-",LONG);
  251.   gen_2op("zulsub","-",ULONG);
  252.   gen_2op("zdsub","-",DOUBLE);
  253.   gen_2op("zlmult","*",LONG);
  254.   gen_2op("zulmult","*",ULONG);
  255.   gen_2op("zdmult","*",DOUBLE);
  256.   gen_2op("zldiv","/",LONG);
  257.   gen_2op("zuldiv","/",ULONG);
  258.   gen_2op("zddiv","/",DOUBLE);
  259.   gen_2op("zlmod","%",LONG);
  260.   gen_2op("zulmod","%",ULONG);
  261.   gen_2op("zllshift","<<",LONG);
  262.   gen_2op("zullshift","<<",ULONG);
  263.   gen_2op("zlrshift",">>",LONG);
  264.   gen_2op("zulrshift",">>",ULONG);
  265.   gen_2op("zland","&",LONG);
  266.   gen_2op("zuland","&",ULONG);
  267.   gen_2op("zlor","|",LONG);
  268.   gen_2op("zulor","|",ULONG);
  269.   gen_2op("zlxor","^",LONG);
  270.   gen_2op("zulxor","^",ULONG);
  271.   gen_2op("zlmod","%",LONG);
  272.   gen_2op("zulmod","%",ULONG);
  273.  
  274.   gen_1op("zlkompl","~",LONG);
  275.   gen_1op("zulkompl","~",ULONG);
  276.  
  277.   gen_cmp("zlleq","<=",LONG);
  278.   gen_cmp("zulleq","<=",ULONG);
  279.   gen_cmp("zdleq","<=",DOUBLE);
  280.   gen_cmp("zpleq","<=",POINTER);
  281.   gen_cmp("zleqto","==",LONG);
  282.   gen_cmp("zuleqto","==",ULONG);
  283.   gen_cmp("zdeqto","==",DOUBLE);
  284.   gen_cmp("zpeqto","==",POINTER);
  285.  
  286.  
  287.   fclose(fin);
  288.   fclose(hout);
  289.   fclose(cout);
  290.   free(have);  
  291.   return 0;
  292. }
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.